<?php

    /**
     * Abstract
     *
     * @package      Core
     * @subpackage   Image_Adapter
     *
     * @license      Open Software License (OSL 3.0) see LICENCE-OSL file or http://opensource.org/licenses/osl-3.0.php
     */
    abstract class Core_Image_Adapter_Abstract
    {

    ////////////////////////////////////////////////////////////////////////////
    //                                Contantes                               //
    ////////////////////////////////////////////////////////////////////////////

        const POSITION_TOP_LEFT       = 'top-left';
        const POSITION_TOP_RIGHT      = 'top-right';
        const POSITION_BOTTOM_LEFT    = 'bottom-left';
        const POSITION_BOTTOM_RIGHT   = 'bottom-right';
        const POSITION_STRETCH        = 'stretch';
        const POSITION_TILE           = 'tile';

    ////////////////////////////////////////////////////////////////////////////
    //                                Variables                               //
    ////////////////////////////////////////////////////////////////////////////

        public $fileName               = null;
        public $imageBackgroundColor   = 0;

        protected $_fileType           = null;
        protected $_fileName           = null;
        protected $_fileMimeType       = null;
        protected $_fileSrcName        = null;
        protected $_fileSrcPath        = null;
        protected $_imageHandler       = null;
        protected $_imageSrcWidth      = null;
        protected $_imageSrcHeight     = null;
        protected $_requiredExtensions = null;
        protected $_watermarkPosition  = null;
        protected $_watermarkWidth     = null;
        protected $_watermarkHeigth    = null;

        protected $_keepAspectRatio;
        protected $_keepFrame;
        protected $_keepTransparency;
        protected $_backgroundColor;
        protected $_constrainOnly;

    ////////////////////////////////////////////////////////////////////////////
    //                                 Methods                                //
    ////////////////////////////////////////////////////////////////////////////

        /**
         * Gets the file MIME type
         *
         * @return      string
         */
        public function getMimeType()
        {
            if ($this->_fileType) {
                return $this->_fileType;
            } else {
                list($this->_imageSrcWidth, $this->_imageSrcHeight, $this->_fileType, ) = getimagesize($this->_fileName);
                $this->_fileMimeType = image_type_to_mime_type($this->_fileType);
                return $this->_fileMimeType;
            }
        }

        /**
         * setWatermarkPosition
         *
         * @param       integer         $position
         * @return      Core_Image
         */
        public function setWatermarkPosition($position)
        {
            $this->_watermarkPosition = $position;
            return $this;
        }

        /**
         * getWatermarkPosition
         *
         * @return      integer
         */
        public function getWatermarkPosition()
        {
            return $this->_watermarkPosition;
        }

        /**
         * setWatermarkWidth
         *
         * @param       integer         $width
         * @return      Core_Image
         */
        public function setWatermarkWidth($width)
        {
            $this->_watermarkWidth = $width;
            return $this;
        }

        /**
         * getWatermarkWidth
         *
         * @return      integer
         */
        public function getWatermarkWidth()
        {
            return $this->_watermarkWidth;
        }

        /**
         * setWatermarkHeigth
         *
         * @param       integer         $heigth
         * @return      Core_Image
         */
        public function setWatermarkHeigth($heigth)
        {
            $this->_watermarkHeigth = $heigth;
            return $this;
        }

        /**
         * getWatermarkHeigth
         *
         * @return      integer
         */
        public function getWatermarkHeigth()
        {
            return $this->_watermarkHeigth;
        }

        /**
         * Get/set keepAspectRatio
         *
         * @param       boolean     $value
         * @return      boolean|Core_Image_Adapter_Abstract
         */
        public function keepAspectRatio($value = null)
        {
            if (null !== $value) {
                $this->_keepAspectRatio = (bool)$value;
            }

            return $this->_keepAspectRatio;
        }

        /**
         * Get/set keepFrame
         *
         * @param       boolean     $value
         * @return      boolean
         */
        public function keepFrame($value = null)
        {
            if (null !== $value) {
                $this->_keepFrame = (bool)$value;
            }

            return $this->_keepFrame;
        }

        /**
         * Get/set keepTransparency
         *
         * @param       boolean     $value
         * @return      boolean
         */
        public function keepTransparency($value = null)
        {
            if (null !== $value) {
                $this->_keepTransparency = (bool)$value;
            }

            return $this->_keepTransparency;
        }

        /**
         * Get/set constrainOnly
         *
         * @param       boolean     $value
         * @return      boolean
         */
        public function constrainOnly($value = null)
        {
            if (null !== $value) {
                $this->_constrainOnly = (bool)$value;
            }

            return $this->_constrainOnly;
        }

        /**
         * Get/set keepBackgroundColor
         *
         * @param array $value
         * @return array
         */
        public function backgroundColor($value = null)
        {
            if (null !== $value) {
                if ((!is_array($value)) || (3 !== count($value))) {
                    return;
                }

                foreach ($value as $color) {
                    if ((!is_integer($color)) || ($color < 0) || ($color > 255)) {
                        return;
                    }
                }
            }

            $this->_backgroundColor = $value;
            return $this->_backgroundColor;
        }

    ////////////////////////////////////////////////////////////////////////////
    //                            Protected Methods                           //
    ////////////////////////////////////////////////////////////////////////////

        /**
         * _getFileAttributes
         *
         * @return      void
         */
        protected function _getFileAttributes()
        {
            $pathinfo = pathinfo($this->_fileName);

            $this->_fileSrcPath = $pathinfo['dirname'];
            $this->_fileSrcName = $pathinfo['basename'];
        }

    ////////////////////////////////////////////////////////////////////////////
    //                            Abstract Methods                            //
    ////////////////////////////////////////////////////////////////////////////

        abstract public function open($fileName);

        abstract public function save($destination = null, $newName = null);

        abstract public function display();

        abstract public function resize($width = null, $height = null);

        abstract public function rotate($angle);

        abstract public function crop($top = 0, $left = 0, $right = 0, $bottom = 0);

        abstract public function watermark($watermarkImage, $positionX = 0, $positionY = 0, $watermarkImageOpacity = 30, $repeat = false);

        abstract public function checkDependencies();
    }